package com.wanlian.jpush.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wanlian.common.JPushComUtil;
import com.wanlian.common.TypeChange;
import com.wanlian.jpush.push.vo.*;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.util.HttpUtils;
import org.springframework.stereotype.Component;

@Component
public class PushService
{

    /**
     * 推送通知单个tag
     * 
     * @param tag
     * @param content
     * @return
     */
    public PushReturnVo sendNotificationBySingleAlias(String alias, String content)
    {
        PushSendVo pushSendVo = getNotificationSendVoByAlias(alias, content);
        // System.out.println(JsonStringUtil.writeValueAsString(pushSendVo));
        PushReturnVo returnVo = this.sendNotification(pushSendVo);
        return returnVo;
    }

    // public static String tag = "924a331309184ab39b48744a6d30590f";
    public String sendUrl = "https://api.jpush.cn/v3/push";

    public PushReturnVo sendNotificationByAll(String content)
    {
        PushSendVo pushSendVo = getNotificationSendVoByAll(content);
        PushReturnVo returnVo = this.sendNotification(pushSendVo);
        return returnVo;
    }

    /**
     * 推送通知多个tag
     * 
     * @param tag
     * @param content
     * @return
     */
    public PushReturnVo sendNotificationByTagList(List<String> tags, String content)
    {
        PushSendVo pushSendVo = getNotificationSendVoByTagList(tags, content);
        PushReturnVo returnVo = this.sendNotification(pushSendVo);
        return returnVo;
    }

    /**
     * 推送通知单个tag
     * 
     * @param tag
     * @param content
     * @return
     */
    public PushReturnVo sendNotificationBySingleTag(String tag, String content)
    {
        PushSendVo pushSendVo = getNotificationSendVoByTag(tag, content);
        System.out.println(JsonStringUtil.writeValueAsString(pushSendVo));
        PushReturnVo returnVo = this.sendNotification(pushSendVo);
        return returnVo;
    }

    /**
     * 推送消息多个tag
     * 
     * @param tag
     * @param content
     * @return
     */
    public PushReturnVo sendMessageByTagList(List<String> tags, String title, String msgContent)
    {
        PushSendVo pushSendVo = getMessageSendVoByTagList(tags, title, msgContent);
        PushReturnVo returnVo = this.sendNotification(pushSendVo);
        return returnVo;
    }

    /**
     * 推送消息单个tag
     * 
     * @param tag
     * @param content
     * @return
     */
    public PushReturnVo sendMessageBySingleTag(String tag, String title, String content)
    {
        PushSendVo pushSendVo = getMessageSendVoByTag(tag, title, content);
        System.out.println(JsonStringUtil.writeValueAsString(pushSendVo));
        PushReturnVo returnVo = this.sendMessage(pushSendVo);
        return returnVo;
    }

    /**
     * 推送通知
     * 
     * @param pushObject
     */
    public PushReturnVo sendNotification(PushSendVo pushSendVo)
    {
        if (!pushSendVo.checkIsAble())
        {
            return null;
        }
        String sendstr = JsonStringUtil.writeValueAsString(pushSendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        headers.put("Content-Length", TypeChange.intToString(sendstr.length()));
        String result = HttpUtils.sendPost(sendUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, PushReturnVo.class);
    }

    /**
     * 推送消息
     * 
     * @param pushObject
     */
    public PushReturnVo sendMessage(PushSendVo pushSendVo)
    {
        if (!pushSendVo.checkIsAble())
        {
            return null;
        }
        String sendstr = JsonStringUtil.writeValueAsString(pushSendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(sendUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, PushReturnVo.class);
    }

    /**
     * 推送内容和消息
     * 
     * @param pushObject
     */
    public PushReturnVo sendNotificationAndMessage(PushSendVo pushSendVo)
    {
        if (!pushSendVo.checkIsAble())
        {
            return null;
        }
        String sendstr = JsonStringUtil.writeValueAsString(pushSendVo);
        Map<String, String> headers = JPushComUtil.getheaders();
        String result = HttpUtils.sendPost(sendUrl, headers, sendstr);
        return JsonStringUtil.readValue(result, PushReturnVo.class);
    }

    private PushSendVo getMessageSendVoByTagList(List<String> tags, String title, String msgContent)
    {

        PushSendVo pushSendVo = new PushSendVo();
        pushSendVo.selPlatFormAll();
        pushSendVo.selAudienceAll();

        Message message = new Message();
        message.setMsgContent(msgContent);
        pushSendVo.setAudience(tags);
        pushSendVo.setMessage(message);

        return pushSendVo;
    }

    private PushSendVo getMessageSendVoByTag(String tag, String title, String msgContent)
    {

        PushSendVo pushSendVo = new PushSendVo();
        pushSendVo.selPlatFormAll();
        pushSendVo.selAudienceAll();

        Message message = new Message();
        message.setMsgContent(msgContent);
        message.setTitle(title);
        List<String> tags = new ArrayList<String>();
        tags.add(tag);
        pushSendVo.setAudience(tags);
        pushSendVo.setMessage(message);

        return pushSendVo;
    }

    private PushSendVo getNotificationSendVoByAll(String content)
    {

        PushSendVo pushSendVo = new PushSendVo();
        pushSendVo.selPlatFormAll();
        pushSendVo.selAudienceAll();
        Notification notification = new Notification();

        /**
         * 发送给android和ios
         */
        NotificationAndroid android = new NotificationAndroid();
        android.setAlert(content);
        NotificationIOS ios = new NotificationIOS();
        ios.setAlert(content);
        ios.setContentavailable(true);
        notification.setAndroidNotification(android);
        notification.setIosNotifycation(ios);

        // pushSendVo.setAudience(tags);
        pushSendVo.setNotification(notification);

        return pushSendVo;
    }

    private PushSendVo getNotificationSendVoByTagList(List<String> tags, String content)
    {

        PushSendVo pushSendVo = new PushSendVo();
        pushSendVo.selPlatFormAll();
        pushSendVo.selAudienceAll();
        Notification notification = new Notification();

        /**
         * 发送给android和ios
         */
        NotificationAndroid android = new NotificationAndroid();
        android.setAlert(content);
        NotificationIOS ios = new NotificationIOS();
        ios.setAlert(content);
        ios.setContentavailable(true);
        notification.setAndroidNotification(android);
        notification.setIosNotifycation(ios);

        pushSendVo.setAudience(tags);
        pushSendVo.setNotification(notification);

        return pushSendVo;
    }

    private PushSendVo getNotificationSendVoByAlias(String alias, String content)
    {

        PushSendVo pushSendVo = new PushSendVo();
        pushSendVo.selPlatFormAll();
        pushSendVo.selAudienceAll();
        Notification notification = new Notification();

        /**
         * 发送给android和ios
         */
        NotificationAndroid android = new NotificationAndroid();
        android.setAlert(content);
        NotificationIOS ios = new NotificationIOS();
        ios.setAlert(content);
        ios.setContentavailable(true);
        notification.setAndroidNotification(android);
        notification.setIosNotifycation(ios);

        List<String> aliaslst = new ArrayList<String>();
        aliaslst.add(alias);
        pushSendVo.setAudienceAlias(aliaslst);
        pushSendVo.setNotification(notification);

        return pushSendVo;
    }

    private PushSendVo getNotificationSendVoByTag(String tag, String content)
    {

        PushSendVo pushSendVo = new PushSendVo();
        pushSendVo.selPlatFormAll();
        pushSendVo.selAudienceAll();
        Notification notification = new Notification();

        /**
         * 发送给android和ios
         */
        NotificationAndroid android = new NotificationAndroid();
        android.setAlert(content);
        NotificationIOS ios = new NotificationIOS();
        ios.setAlert(content);
        ios.setContentavailable(true);
        notification.setAndroidNotification(android);
        notification.setIosNotifycation(ios);

        List<String> taglist = new ArrayList<String>();
        taglist.add(tag);
        pushSendVo.setAudience(taglist);
        pushSendVo.setNotification(notification);

        return pushSendVo;
    }

    public static void main(String[] args)
    {
        // PushSendVo=new PushSendVo().get
        // System.out.println(JsonStringUtil.writeValueAsString(pushSendVo));
        // PushReturnVo returnVo = new
        // PushService().sendNotification(pushSendVo);
        // if (returnVo != null) {
        // System.out.println(JsonStringUtil.writeValueAsString(returnVo));
        // } else {
        // System.out.println("发送参数异常");
        // }
        // new
        // PushService().sendNotificationBySingleTag("924a331309184ab39b48744a6d30590f",
        // "测试tag");
        // new PushService().sendNotificationBySingleTag("123456", "测试tag");
        // new PushService().sendMessageBySingleTag("123456", "消息", "测试消息");
        // new PushService().sendNotificationBySingleTag(tag, content)
        // new PushService().sendNotificationByAll("侧搜tag");
        // new
        // PushService().sendNotificationBySingleAlias("2293be55848540609efb1342b6ad9ff0",
        // "测试消息");
        new PushService().sendNotificationByAll("测试消息");
    }
}
